import { resolve } from 'path'
import { defineConfig, type PluginOption } from 'vite'
import Vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import Inspect from 'vite-plugin-inspect'
import Checker from 'vite-plugin-checker'
// import basicSsl from '@vitejs/plugin-basic-ssl'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import graphql from '@rollup/plugin-graphql'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { compression } from 'vite-plugin-compression2'
import { visualizer } from 'rollup-plugin-visualizer'
// import ViteCDNPlugin from 'vite-plugin-cdn-import'
// import VueDevTools from 'vite-plugin-vue-devtools'

export default defineConfig({
  plugins: [
    Vue(),
    UnoCSS(),
    Inspect(),
    Checker({ typescript: false }),
    // 配置https证书
    // basicSsl(),
    AutoImport({
      dts: true,
      resolvers: [ElementPlusResolver()],
      imports: ['vue', 'vue-router', 'pinia'],
      eslintrc: { enabled: false } // 一旦生成.eslintrc-auto-import配置文件之后，最好把enable关掉，否则这个文件每次会在重新加载的时候重新生成，这会导致eslint有时找不到这个文件，当需要更新配置文件的时候再重新打开
    }),
    Components({
      dts: true,
      resolvers: [ElementPlusResolver()]
    }),
    graphql(),
    compression({
      deleteOriginalAssets: true, // 是否删除原有资产
      threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b，1b=8B, 1B=1024KB  这里相当于 9kb多吧，就会压缩
      algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
      include: [/\.(js|css)/]
    }),
    visualizer({
      emitFile: false, // 是否生成到打包文件中，false：生成到项目根目录
      filename: 'stats.html', // 分析图生成的文件名
      open: false, // 如果存在本地服务端口，将在打包后自动展示
      template: 'treemap' // 可选值: treemap, sunburst, network, raw-data, list
    }) as unknown as PluginOption
    // ViteCDNPlugin({
    //   modules: [
    //     {
    //       name: 'lodash',
    //       var: '_',
    //       path: `https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js`
    //     }
    //   ]
    // })
    // VueDevTools()
  ],
  server: {
    proxy: {
      // http://localhost:5173/api/bar -> http://localhost:3000/api/bar
      '/api': {
        target: 'http://127.0.0.1:3000',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    }
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vue: ['vue', 'vue-router', 'pinia', 'pinia-plugin-persist'],
          axios: ['axios'],
          elementPlus: ['element-plus'],
          other: ['unocss']
        }
      }
    }
  }
  // esbuild: {
  //   drop: mode === 'wcm' || mode === 'production' ? ['debugger', 'console'] : [],
  // }
})
